home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / security / DigestInputStream.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  5.5 KB  |  181 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)DigestInputStream.java    1.29 98/06/29
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.security;
  16.  
  17. import java.io.IOException;
  18. import java.io.EOFException;
  19. import java.io.InputStream;
  20. import java.io.FilterInputStream;
  21. import java.io.PrintStream;
  22. import java.io.ByteArrayInputStream;
  23.  
  24. /**
  25.  * A transparent stream that updates the associated message digest using
  26.  * the bits going through the stream.
  27.  *
  28.  * <p>To complete the message digest computation, call one of the
  29.  * <code>digest</code> methods on the associated message
  30.  * digest after your calls to one of this digest input stream's
  31.  * {@link read() read} methods.
  32.  *
  33.  * <p>It is possible to turn this stream on or off (see
  34.  * {@link on(boolean) on}). When it is on, a call to one of the
  35.  * <code>read</code> methods
  36.  * results in an update on the message digest.  But when it is off,
  37.  * the message digest is not updated. The default is for the stream
  38.  * to be on.
  39.  *
  40.  * <p>Note that digest objects can compute only one digest (see
  41.  * {@link MessageDigest}),
  42.  * so that in order to compute intermediate digests, a caller should
  43.  * retain a handle onto the digest object, and clone it for each
  44.  * digest to be computed, leaving the orginal digest untouched.
  45.  *
  46.  * @see MessageDigest
  47.  *
  48.  * @see DigestOutputStream
  49.  *
  50.  * @version 1.29 99/03/26
  51.  * @author Benjamin Renaud
  52.  */
  53.  
  54. public class DigestInputStream extends FilterInputStream {
  55.  
  56.     /* NOTE: This should be made a generic UpdaterInputStream */
  57.  
  58.     /* Are we on or off? */
  59.     private boolean on = true;
  60.  
  61.     /**
  62.      * The message digest associated with this stream.
  63.      */
  64.     protected MessageDigest digest;
  65.  
  66.     /**
  67.      * Creates a digest input stream, using the specified input stream
  68.      * and message digest.
  69.      *
  70.      * @param stream the input stream.
  71.      *
  72.      * @param digest the message digest to associate with this stream.
  73.      */
  74.     public DigestInputStream(InputStream stream, MessageDigest digest) {
  75.     super(stream);
  76.     setMessageDigest(digest);
  77.     }
  78.  
  79.     /**
  80.      * Returns the message digest associated with this stream.
  81.      *
  82.      * @return the message digest associated with this stream.
  83.      */
  84.     public MessageDigest getMessageDigest() {
  85.     return digest;
  86.     }
  87.  
  88.     /**
  89.      * Associates the specified message digest with this stream.
  90.      *
  91.      * @param digest the message digest to be associated with this stream.
  92.      */
  93.     public void setMessageDigest(MessageDigest digest) {
  94.     this.digest = digest;
  95.     }
  96.  
  97.     /**
  98.      * Reads a byte, and updates the message digest (if the digest
  99.      * function is on).  That is, this method reads a byte from the
  100.      * input stream, blocking until the byte is actually read. If the
  101.      * digest function is on (see {@link on(boolean) on}), this method
  102.      * will then call <code>update</code> on the message digest associated
  103.      * with this stream, passing it the byte read.
  104.      *
  105.      * @return the byte read.
  106.      *
  107.      * @exception IOException if an I/O error occurs.
  108.      *
  109.      * @see MessageDigest#update(byte)
  110.      */
  111.     public int read() throws IOException {
  112.     int ch = in.read();
  113.     if (on && ch != -1) {
  114.         digest.update((byte)ch);
  115.     }
  116.     return ch;
  117.     }
  118.  
  119.     /**
  120.      * Reads into a byte array, and updates the message digest (if the
  121.      * digest function is on).  That is, this method reads up to
  122.      * <code>len</code> bytes from the input stream into the array
  123.      * <code>b</code>, starting at offset <code>off</code>. This method
  124.      * blocks until the data is actually
  125.      * read. If the digest function is on (see
  126.      * {@link on(boolean) on}), this method will then call <code>update</code>
  127.      * on the message digest associated with this stream, passing it
  128.      * the data.
  129.      *
  130.      * @param b    the array into which the data is read.
  131.      *
  132.      * @param off the starting offset into <code>b</code> of where the
  133.      * data should be placed.
  134.      *
  135.      * @param len the maximum number of bytes to be read from the input
  136.      * stream into b, starting at offset <code>off</code>.
  137.      *
  138.      * @return  the actual number of bytes read. This is less than
  139.      * <code>len</code> if the end of the stream is reached prior to
  140.      * reading <code>len</code> bytes. -1 is returned if no bytes were
  141.      * read because the end of the stream had already been reached when
  142.      * the call was made.
  143.      *
  144.      * @exception IOException if an I/O error occurs.
  145.      *
  146.      * @see MessageDigest#update(byte[], int, int)
  147.      */
  148.     public int read(byte[] b, int off, int len) throws IOException {
  149.     int result = in.read(b, off, len);
  150.     if (on && result != -1) {
  151.         digest.update(b, off, result);
  152.     }
  153.     return result;
  154.     }
  155.  
  156.     /**
  157.      * Turns the digest function on or off. The default is on.  When
  158.      * it is on, a call to one of the <code>read</code> methods results in an
  159.      * update on the message digest.  But when it is off, the message
  160.      * digest is not updated.
  161.      *
  162.      * @param on true to turn the digest function on, false to turn
  163.      * it off.
  164.      */
  165.     public void on(boolean on) {
  166.     this.on = on;
  167.     }
  168.  
  169.     /**
  170.      * Prints a string representation of this digest input stream and
  171.      * its associated message digest object.
  172.      */
  173.      public String toString() {
  174.      return "[Digest Input Stream] " + digest.toString();
  175.      }
  176. }
  177.  
  178.  
  179.  
  180.  
  181.